<script setup>
import { getConfig, saveConfig } from "@/api/system/config";

const props = defineProps({
  id: {
    type: [Number, String],
    default: ""
  }
});

const formRef = ref();
const data = ref({
  loading: false,
  form: {
    id: props.id
  },
  rules: {}
});

onMounted(() => {
  if (data.value.form.id != "") {
    getInfo();
  }
});

function getInfo() {
  data.value.loading = true;
  getConfig(props.id).then((res) => {
    data.value.loading = false;
    data.value.form = res.data;
  });
}

defineExpose({
  submit(callback) {
    if (data.value.form.id == "") {
      formRef.value.validate((valid) => {
        if (valid) {
          saveConfig(data.value.form).then(() => {
            ElMessage.success({
              message: "保存成功",
              center: true
            });
            callback && callback();
          });
        }
      });
    } else {
      formRef.value.validate((valid) => {
        if (valid) {
          saveConfig(data.value.form).then(() => {
            ElMessage.success({
              message: "修改成功",
              center: true
            });
            callback && callback();
          });
        }
      });
    }
  }
});
</script>

<template>
  <div v-loading="data.loading">
    <el-form
      ref="formRef"
      :model="data.form"
      :rules="data.rules"
      label-width="120px"
      label-suffix=" "
      label-position="top"
      style="padding: 0 20px"
    >
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="参数名称" prop="configName">
            <el-input
              placeholder="输入参数名称"
              v-model="data.form.configName"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="参数编码" prop="configKey">
            <el-input placeholder="输入参数编码" v-model="data.form.configKey"
          /></el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="参数值" prop="configValue">
            <el-input
              placeholder="输入参数值"
              v-model="data.form.configValue"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否内置参数" prop="configType">
            <el-radio-group v-model="data.form.configType">
              <el-radio-button label="Y">是</el-radio-button>
              <el-radio-button label="N">否</el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input
              placeholder="输入参数值"
              v-model="data.form.remark"
              type="textarea"
              :rows="3"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
